package com.new2i.service.impl;
 
import java.util.ArrayList;
import java.util.HashMap;
  
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.new2i.dao.able.UserDao; 
import com.new2i.model.UserEntity;
import com.new2i.service.able.UserService;
import com.new2i.util.Collection2JsonUtil; 

/**
 *  用户相关业务逻辑处理实现类
 * 
 * @since 2015/08/21
 * @author 2i
 *
 */
@Service("UserService")
public class UserServiceImpl implements UserService {
     
	@Autowired
	private UserDao mUserDao;

	private static Logger logger = Logger.getLogger(UserServiceImpl.class);
	  
	@Override
	public HashMap<String, Object> getUserInfoById(int user_id) {
		HashMap<String, Object> sHashMap = new HashMap<String,Object>(); 
		if(user_id > 0) { 
			sHashMap =  mUserDao.getUserInfoById(user_id);
		}
		return  sHashMap;
	}

    @Override
    public List<UserEntity> getUserIdByCity(String city) {
        if(city==null||city.length()<=0){
            return null;
        }
        return mUserDao.getUserIdByCity(city);
    }

	@Override
	public List<HashMap<String, Object>> selectContactListByWxid(String wxid) { 
		List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>(); 
		if(wxid == null || wxid.length() <= 0)
			return list; 
		list = mUserDao.selectUserInfoByWxid(wxid, 3, -1); 
		return list; 
	}

	@Override
	public HashMap<String, Object> getUserInfoByWxCode(String wxid) {
		HashMap<String, Object> sHashMap = new HashMap<String,Object>(); 
		if(wxid == null || wxid.length() <= 0){
			return sHashMap;
		}
		//根据wxid返回用户信息
		List<HashMap<String, Object>> rlist = mUserDao.selectUserInfoByWxid(wxid,3,0); 
		if(rlist == null || rlist.size() <= 0)
			return sHashMap;  
		sHashMap = rlist.get(0); 
		return sHashMap;
	}

	@Override
	public String updateUserInfoByUserId(UserEntity user) {
		if (user == null || user.getUser_id() <= 0) {
			return Collection2JsonUtil.byErrInfo(310, "user_id 参数错误");
		} 
		try {
			int updateRows = mUserDao.updateUserInfoByUserId(user); 
			logger.debug("updateUserInfoByUserId updateRows==>" + updateRows);
		}catch (Exception e) {
			logger.error("updateUserInfoByUserId==>"+e.getLocalizedMessage()); 
			return Collection2JsonUtil.byErrInfo(320, "修改个人信息失败");
		}
		return Collection2JsonUtil.byMsgInfo("修改个人信息成功"); 
	}  
}
